package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.controller.OtherCtlr;
import org.gov.vis.master.controller.OtherCtlrImpl;
import org.gov.vis.master.dto.OtherDto;
import org.gov.vis.master.gui.EditOthersDialog;
import org.gov.vis.master.gui.ViewOthersDialog;
import org.gov.vis.util.Util;

public class ViewOthersListener implements ActionListener,BaseListener {
	
	private OtherCtlr otherCtlr = new OtherCtlrImpl();
	private ViewOthersDialog viewOthersDialog;
	private OtherDto otherDto;
	final private ResourceBundle rb = Util.getResources();
	private EditOthersDialog editOthersDialog;

	public ViewOthersListener(ViewOthersDialog viewOthersDialog) {
		// TODO Auto-generated constructor stub
		this.viewOthersDialog = viewOthersDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewOthersDialog _viewOthersDialog =(ViewOthersDialog)_guiObject;
		otherDto = new OtherDto();
		
		String code = _viewOthersDialog.getCodeTxtBx().getText();
		if(code!=null){
		 otherDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		OtherDto _otherDto = (OtherDto)_dtoObject;
		
		if(editOthersDialog!=null){
			editOthersDialog.getTxtFldCode().setText(_otherDto.getCode());
			editOthersDialog.getTxtFldAIWorker().setText(_otherDto.getNameOfAIWorker());
			editOthersDialog.getTxtFldDistrict().setText(_otherDto.getDistrict());
			editOthersDialog.getTxtFldHeadQuarter().setText(_otherDto.getHeadQuarter());
			editOthersDialog.getTxtFldJurisdiction().setText(_otherDto.getJuridiction());
			editOthersDialog.getTxtFldMandal().setText(_otherDto.getMandal());
			editOthersDialog.getTxtFldOrg().setText(_otherDto.getNameOfOrg());
			editOthersDialog.setHidnObject(_otherDto.getOtherId());
		}
	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewOthersDialog);
			try {
				
			List<OtherDto> othersLst = otherCtlr.searchOthers(otherDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Organisation");
			colLst.add("AI Worker");
			colLst.add("Head Quarters");
			colLst.add("Judstriction");
			//colLst.add("Mandal");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(othersLst != null && !othersLst.isEmpty()){
				for(OtherDto dto:othersLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getNameOfOrg());
					lst.add(dto.getNameOfAIWorker());
					lst.add(dto.getHeadQuarter());
					lst.add(dto.getJuridiction());
					//lst.add(dto.getJuridiction());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewOthersDialog.getPnlOthersResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			OtherDto otherDto = (OtherDto)visHyperLink.getObject();
		 	editOthersDialog = new EditOthersDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_othr_title"),
                    false);
		 	
		 	editOthersDialog.setSearchText(viewOthersDialog.getCodeTxtBx().getText());
		 	
			setDtoDetailsToGui(otherDto);
		   viewOthersDialog.dispose();
			
		}
		
	}

}
